package com.guava.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.security.acl.Owner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.AbstractHttpConnection.Output;

import config.GHelper;
import config.FreemarkerHelp;
public class Loader extends HttpServlet {

	private static final long serialVersionUID = -1915463532411657451L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			// Write some content
			Router router = new Router(request);
			if (router.p()) {
//				out.println(router.getController());
//				out.println(router.getAction());

				
				try {
					Class onwClass = Class.forName("controll."+router.getController());
					Object o = onwClass.newInstance();
					Method method = onwClass.getMethod(router.getAction()); // 此时获得了add方法
					
					
					FreemarkerHelp.setInstance(getServletContext(),router);
					GHelper.r=router;
					GHelper.request=request;
					GHelper.response=response;
					GHelper.servletContext=getServletContext();
					Object result = method.invoke(o); // 自动装箱
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				
			}else {
				out.println("URL 格式错误");
			}

//			out.print(request.getParameter("id"));
		} finally {
			if(out!=null){
				out.close();
			}
			
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Do some other work
		doGet(request, response);
	}

	@Override
	public String getServletInfo() {
		return "MyFirstServlet";
	}
}